package top.smallway.lightbandassistant_app.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import top.smallway.lightbandassistant_app.R;
import top.smallway.lightbandassistant_app.UI.Author_Songs;
import top.smallway.lightbandassistant_app.data.Authors;

/**
 * 谱师列表适配器
 * 负责显示谱师列表，实现谱师头像和名称的展示，以及点击跳转到谱师作品页面的功能
 */
public class AuthorsAdapter extends RecyclerView.Adapter<AuthorsAdapter.MyViewHolder> {
    /**
     * 构造函数
     * @param data 谱师数据列表
     * @param context 上下文对象
     */
    public AuthorsAdapter(List<Authors> data, Context context) {
        this.data = data;
        this.context = context;
    }

    // 谱师数据列表
    private List<Authors> data;
    // 上下文对象
    private final Context context;
    // 是否启用加载更多功能
    private boolean mIsLoadMoreEnabled = false;


    @NonNull
    /**
     * 创建ViewHolder
     * @param parent 父视图组
     * @param viewType 视图类型
     * @return 返回创建的ViewHolder
     */
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.author, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint({"ResourceAsColor", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.author.setText(data.get(position).getName());
        Glide.with(holder.avatar).load(data.get(position).getAvatar()).transform(new CenterCrop()).error(R.drawable.avatar).into(holder.avatar);
        holder.linearlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Author_Songs.class);
                intent.putExtra("id", data.get(position).getId());
                intent.putExtra("avatar", data.get(position).getAvatar());
                intent.putExtra("name", data.get(position).getName());
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return mIsLoadMoreEnabled ? (data == null ? 1 : data.size() + 1) : (data == null ? 0 : data.size());
    }

    /**
     * 视图持有者类
     * 用于缓存谱师项的视图组件
     */
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView author;
        private ImageView avatar;
        private LinearLayout linearlayout;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            author = itemView.findViewById(R.id.author);
            avatar = itemView.findViewById(R.id.avatar);
            linearlayout=itemView.findViewById(R.id.linearlayout);

        }
    }
}
